﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Mime;
using UIFrameWork;

public class UIEventManger : Singleton<UIEventManger>
{
    
    public delegate void OnUIEventHandler(UIEvent uiEvent);
    private UIEventManger.OnUIEventHandler[] m_uiEventHandlerMap = new OnUIEventHandler[1000];//根据eventid调整
    private List<object> m_uiEvents = new List<object>();

    public bool HasUIEventListener(enUIEventID eventId)
    {
        return this.m_uiEventHandlerMap[(int) eventId] == null;
    }

    public void AddUIEventListener(enUIEventID eventId, UIEventManger.OnUIEventHandler onUiEventHandler)
    {
        if (this.m_uiEventHandlerMap[(int) eventId] == null)
        {
            this.m_uiEventHandlerMap[(int) eventId] = delegate { };
            UIEventManger.OnUIEventHandler[] uieventhadlemap = this.m_uiEventHandlerMap;
            int intptr = (int) eventId;
            uieventhadlemap[intptr] =
                (UIEventManger.OnUIEventHandler) Delegate.Combine(uieventhadlemap[intptr], onUiEventHandler);
        }
        else
        {
            UIEventManger.OnUIEventHandler[] uiEventHandlers1 = this.m_uiEventHandlerMap;
            int intptr = (int) eventId;
            uiEventHandlers1[intptr] =(UIEventManger.OnUIEventHandler) Delegate.Remove(uiEventHandlers1[intptr], onUiEventHandler);
            UIEventManger.OnUIEventHandler[] uiEventHandlers2 = this.m_uiEventHandlerMap;
            int intptr2 = (int) eventId;
            uiEventHandlers2[intptr2] =(UIEventManger.OnUIEventHandler) Delegate.Remove(uiEventHandlers2[intptr2], onUiEventHandler);
            
        }
    }

    public void RemoveUIEventListener(enUIEventID eventId,UIEventManger.OnUIEventHandler onUiEventHandler)
    {
        if (this.m_uiEventHandlerMap[(int) eventId] != null)
        {
            UIEventManger.OnUIEventHandler[] uievents = this.m_uiEventHandlerMap;
            int intptr = (int) eventId;
            uievents[intptr] = (UIEventManger.OnUIEventHandler)Delegate.Remove(uievents[intptr], onUiEventHandler);
        }
    }

    public void DispathUIEvent(UIEvent uiEvent)
    {
        uiEvent.isUse = true;
        UIEventManger.OnUIEventHandler callback = this.m_uiEventHandlerMap[(int) uiEvent.m_eventID];
        if (callback != null)
        {
            callback(uiEvent);
        }
        uiEvent.Clear();
    }

    public void DispathUIEvent(enUIEventID eventId)
    {
        UIEvent uiEvent = GetEvent();
        uiEvent.m_eventID = eventId;
        DispathUIEvent(uiEvent);
    }

    public void DispathUIEvent(enUIEventID eventId,stUIEventParms parms)
    {
        UIEvent uiEvent = GetEvent();
        uiEvent.m_eventID = eventId;
        uiEvent.m_evemtParams = parms;
        DispathUIEvent(uiEvent);
    }
    public UIEvent GetEvent()
    {
        for (int i = 0; i < m_uiEvents.Count; i++)
        {
            UIEvent uiEvent = (UIEvent) m_uiEvents[i];
            if (uiEvent.isUse == false)
            {
                return uiEvent;
            }
        }
        UIEvent uiEvent2 = new UIEvent();
        this.m_uiEvents.Add(uiEvent2);
        return uiEvent2;
    }
}











